package com.servlet;

import com.entity.dto.ApiResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import java.io.File;
import java.io.IOException;

@RequestMapping
@RestController
@CrossOrigin
@MultipartConfig
public class DinerPicture {
    @RequestMapping("/uploadPicture")
    public ApiResult upload(MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            String contentType = file.getContentType();
            if (!contentType.equals("image/jpeg")  && !contentType.equals("image/png") && !contentType.equals("image/jpg") && !contentType.equals("image/gif") && !contentType.equals("image/webp")){
                return new ApiResult(500,"图片不符合格式","String",null);

            }else {
                file.transferTo(new File("/app/apache-tomcat-8.5.93/webapps/img/" + file.getOriginalFilename()));
                return  new ApiResult(200,"上传成功","String",file.getOriginalFilename());
            }
        }
        return new ApiResult(500,"上传的不是图片","String",null);
    }
}

